from flaskDemo import create_app,socketio,emit
from flask import request
import redis
app = create_app();
redis_client = redis.Redis(host='127.0.0.1', port=6379, db=0, max_connections=10)
connected_sids = set()
@socketio.on('message',namespace="/")
def message(message):
    print(message)
    userId = message.get("userId")
    sid = message.get("socketId")
    redis_client.set(userId, sid)
    redis_client.set(sid, userId)
    print(redis_client.get(sid))
    socketio.emit('test3', {'data': '服务器链接成功'})

@socketio.on('connect',namespace="/")
def connect():
    connected_sids.add(request.sid)
    print("服务器链接成功")
    socketio.emit('connection', {'data': '服务器链接成功'})


@socketio.on('disconnect', namespace="/")
def test_disconnect():
    connected_sids.remove(request.sid)
    print('服务器断开链接')
    socketio.emit('disconnect', {'data': '服务器链接断开'})

if __name__ == "__main__":
    app.run(debug=True)
    # socketio.run(app, host='127.0.0.1', port=5000, debug=True,allow_unsafe_werkzeug=True)
